package com.woniu.adapter;

import com.woniu.outlet.dao.ClientApplicationsMapper;
import com.woniu.outlet.dao.PotentialMessageMapper;
import com.woniu.outlet.po.ClientApplications;
import com.woniu.outlet.po.PotentialCustomers;
import com.woniu.outlet.po.PotentialMessage;
import com.woniu.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Component
@Transactional
public class ClientApplicationAdapter {

    @Autowired
    private ClientApplicationsMapper clientApplicationsMapper;

    @Autowired
    private PotentialMessageMapper potentialMessageMapper;

    //客户经理自己来申请，最后由销售经理来审批
    public int applications(ClientApplications clientApplications){
        int applications = clientApplicationsMapper.applications(clientApplications);
        return applications;
    }

    //首先经理可以查看所有的没有处理的申请书
    public List<ClientApplications> findapplications(){
        List<ClientApplications> findapplicationlist = clientApplicationsMapper.findapplications();
        return findapplicationlist;
    }

    //营销经理来处理发过来的申请(默认经理是同意的),这个时侯经理给客户经理发送手机消息，需要使用到队列
    public int handleapplications(ClientApplications clientApplications){
        //获得该客户对象的id
        Integer managerid=clientApplications.getClientmanager_Id();

        //创建一个message对象封装数据到对象当中
        PotentialMessage potentialMessage = new PotentialMessage();
        potentialMessage.setPotentialmessageExchange("potentialcustomer_exchange");
        potentialMessage.setPotentialmessageKey("potentialcustomer.xxx");
        potentialMessage.setPotentialmessageCount(10);
        potentialMessage.setPotentialmessageContext(""+managerid+","+"handleapplications");
        potentialMessage.setPotentialmessageStatus(0);
        //将这个message存到数据库
        potentialMessageMapper.insert(potentialMessage);

        //首先修改申请表的状态
        Integer app_id = clientApplications.getApp_Id();
        clientApplicationsMapper.updatestatus(app_id);

        //修改潜在客户表的状态和管户人
        Integer clientmanager_id = clientApplications.getClientmanager_Id();
        Integer app_potentialcustomers_id = clientApplications.getApp_Potentialcustomers_Id();
        int i = clientApplicationsMapper.updatePotentialstatus(clientmanager_id, app_potentialcustomers_id);

        return i;
    }

    //客户经理发来的申请，营销经理没有同意，拒绝了
    public Integer rejectapplication(Integer app_id){
        Integer rejectapplication = clientApplicationsMapper.rejectapplication(app_id);
        return rejectapplication;

    }


    //第二种管理潜在客户的方法：是由营销经理直接对潜在客户进行分配
    // ，这个也涉及到分布式事务的一致性！！     在营销经理分配完之后，就会给客户经理发送短信消息！！！！！
    public Integer distributionpotentialcustomer(PotentialCustomers potentialCustomers) {
        Integer distributionpotentialcustomer = clientApplicationsMapper.distributionpotentialcustomer(potentialCustomers);
        //创建一个消息对象
        PotentialMessage potentialMessage = new PotentialMessage();
        potentialMessage.setPotentialmessageExchange("potentialcustomer_exchange");
        potentialMessage.setPotentialmessageKey("potentialcustomer.xxx");
        potentialMessage.setPotentialmessageCount(10);
        potentialMessage.setPotentialmessageContext(""+potentialCustomers.getPcworkerid()+","+"distributionpotentialcustomer");
        potentialMessage.setPotentialmessageStatus(0);
        //将这个message存到数据库
        potentialMessageMapper.insert(potentialMessage);

        return distributionpotentialcustomer;
    }



}
